var enquiry_view = {
	init:function()
	{
		var $price_btn = $('#price-do-box').find('.price-do-btn');
		if($price_btn.length > 0){
			$price_btn.each(function(i, e) {
				$(this).bind('click',function(){
					enquiry_view.priceDo($(this));
				});
                
            });
		}
		
		var $price_view_btn = $('#price-do-box').find('.price-view-btn');
		if($price_view_btn.length > 0){
			$price_view_btn.each(function(i, e) {
				$(this).bind('click',function(){
					enquiry_view.viewPrice($(this));
				});
                
            });
		}
		
		var $callback = $('#callback-href');
		if($callback.length > 0){
			$callback.bind('click',function(){
					history.back(-1);
			});
		}
	
	},
	priceDo:function($self){
		var pid = eid = units = '';
		pid = $self.data('pid');
		eid = $self.data('enqid');
		units = $self.data('unit');
		
		var unitlist =  ['','元/m2','元/m2','元/个','元/个','元/m','元/m2']
		
		
		if(!pid){
			return false;
		}
		
		var strUnit = '';
		if(units){
			strUnit = unitlist[units];
		}else{
			units = 1
			strUnit = unitlist[1];
		}
		
		//弹窗
		var htmlContent = '';
		
		
		htmlContent += '<form class="form-horizontal">';
		htmlContent+= '<div class="form-group">';
		htmlContent+= '<label  class="col-sm-2 control-label">报价:</label>';
		htmlContent+= '<div class="col-sm-10">';
		htmlContent+= '<input class="form-control form-price" id="price-inp" placeholder="输入报价价格" type="text">';
		htmlContent+= '<input type="hidden" value= "'+units+'" id="unit-sel" />' ;
		htmlContent+= '<div  style=" float: left;height: 34px;line-height: 35px;margin-left: 9px;width: 37px;">'+strUnit+'</div>';
		
	
		
		htmlContent+= '</div>';
		htmlContent+= '</div>';
		htmlContent+= '<div class="form-group">';
		htmlContent+= ' <label  class="col-sm-2 control-label">附加说明:</label>';
		htmlContent+= '<div class="col-sm-10">';
		htmlContent+= ' <textarea  id="remak-inp" class="form-control form-remak" ></textarea>';
		htmlContent+= '</div>';
		htmlContent+= '</div>';
		
		htmlContent+= ' </form>';
	
		layer.open({
		  type: 1,
		  title: '报价',
		  closeBtn: 1,
		  btnAlign: 'c', //按钮居中
		  btn: ['确定', '取消'],
		  area: ['600px', '300px'],
		  shadeClose: true,
		  content: htmlContent,
		  yes: function(){
             enquiry_view.offerDo(pid,eid)
          }
		});

	},
	offerDo:function(pid,eid){
		var $price = $('#price-inp');
		var $remak = $('#remak-inp');
		var $unit = $('#unit-sel');
		var priceVal = remakVal = unitVal= '';
		priceVal = $price.val();
		remakVal = $remak.val();
		unitVal = $unit.val();
		if(priceVal==''){
			alert('请输入报价价格!');
			$price.focus();
			return false;
		}
		var testPrice = /^\d{1,18}(.\d{1,2})?$/gi.test(priceVal);
		if(!testPrice){
			alert('输入报价价格格式不正确!');
			$price.focus();
			return false;
		}
		//报价处理
		$.post("/admins/enquiry_manage.html",{'cmd':'offerdo','pid':pid,'eid':eid,'price':priceVal,'remak':remakVal,'unit':unitVal},function(data) {
			   if(data=='ok'){
				   alert('报价成功');
				   layer.closeAll();
				   document.location.reload();
			   }else if(data=='error'){
				   alert('操作失败');
			   }
        });
		
	},
	viewPrice:function($self){
		var pid = '';
		var units = 0;
		
		pid = $self.data('pid');
		units = $self.data('unit');
		var unitlist =  ['','元/m2','元/m2','元/个','元/个','元/m','元/m2']
		
		
		var htmlContent = '';
		//报价处理
		$.post("/admins/enquiry_manage.html",{'cmd':'getpriceinfo','pid':pid,'unit':units},function(data) {
			   if(data=='error'){
				   alert('操作失败');
			   }else{
				    htmlContent = data
				   	layer.open({
					  type: 1,
					  title: '报价详情',
					  closeBtn: 1,
					  area: ['500px', '300px'],
					  shadeClose: true,
					  content: htmlContent
					});
			   }
        });
		
		
	}
}

$(function(){
    enquiry_view.init();
});